import datetime
import json
import math
import random
import requests
from flask import Blueprint, render_template, request, jsonify, current_app

from applications.common import curd
from applications.common.curd import model_to_dicts, enable_status, disable_status
from applications.common.helper import ModelFilter
from applications.common.utils.http import table_api, fail_api, success_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import xss_escape
from applications.extensions import db

from applications.models import EquipmentCameras
from applications.schemas import PoliceOutSchema, TrainOutSchema,TrainDataSchema

equipments_api = Blueprint('adminequipmentsapi', __name__, url_prefix='/admin/equipments_api')


#模拟课题二提供体征数据
@equipments_api.post('/start')
def post_example():
    data = request.json
    if data.get('body') == 'start':
        signs={
        "Blood_oxygen" : str(round(random.uniform(96, 98), 2)),
        "blood_pressure" : str(random.randint(110, 115)),
        "Respiratory_rate" : str(random.randint(18, 26)),
        "heart_rate" : str(random.randint(65, 80)),
        "temperature" : str(round(random.uniform(36.2, 36.7), 2))
        }
        return jsonify(signs)
    else:
        return jsonify({'message': 'fail'})

@equipments_api.get('/get_cameras')
def test_equipments():
    cameras = get_equipments_cameras()
    return jsonify(code=200,msg="OK",data={"cameras":cameras})

def get_equipments_cameras():
    cameras_body = {
        "page":0,                #第几页 从0开始 必选参数
        "page_size":10,          #每页条数 必选参数

    }
    cameras = requests.post(url=current_app.config['EMERGENCY_URL'] + '/cameras',json=cameras_body).json()['data']
    return cameras
    # for camera in cameras:
    #     id = camera.get('id')
    #     name = camera.get('name')
    #     x = camera.get('x')
    #     y = camera.get('y')
    #     z = camera.get('z')
    #     rx = camera.get('rx')
    #     ry = camera.get('ry')
    #     rz = camera.get('rz')
    #     ip = camera.get('ip')
    #     port = camera.get('port')
    #     channel = camera.get('channel')
    #     username = camera.get('username')
    #     password = camera.get('password')
    #     rtsp = camera.get('rtsp')
    #     map_id = camera.get('map_id')
    #     dept_id = camera.get('dept_id')
    #     dept_parent_name = camera.get('dept_parent_name')
    #     dept_name = camera.get('dept_name')
    #     record = camera.get('record')
    #     record = EquipmentCameras(id=id, name=name, x=x, y=y, z=z, rx=rx, ry=ry, rz=rz, ip=ip, port=port,
    #                               channel=channel, \
    #                               username=username, password=password, rtsp=rtsp, map_id=map_id, dept_id=dept_id, \
    #                               dept_parent_name=dept_parent_name, dept_name=dept_name)
    #     db.session.add(record)
    #     db.session.commit()
